package xin.marcher.module.message.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Repository;
import xin.marcher.module.message.domain.entity.DataRefreshDetailDO;
import xin.marcher.module.message.mapper.DataRefreshMapper;

import javax.annotation.Resource;

/**
 * 
 */
@Repository
public class DataRefreshRepository {


    @Resource
    private DataRefreshMapper dataRefreshMapper;

    /**
     * 获取得到消息最近消费时间的记录(默认查询大于这个时间点大于1分钟的数据)
     *
     * @param page       分页对象
     * @param createDate 消息最新的消费时间
     * @return
     */
    public Page<DataRefreshDetailDO> queryDataRefreshDetailDOList(Page<DataRefreshDetailDO> page, String createDate) {
        LambdaQueryWrapper<DataRefreshDetailDO> queryWrapper = Wrappers.lambdaQuery();
        // 查询创建时间大于这个版本号且未处理的数据
        queryWrapper.gt(DataRefreshDetailDO::getVersion, createDate);
        queryWrapper.eq(DataRefreshDetailDO::getCacheStatus, 0);
        // 按版本号进行排序
        queryWrapper.orderByDesc(DataRefreshDetailDO::getVersion);
        return dataRefreshMapper.selectPage(page, queryWrapper);
    }

    /**
     * 删除超过一定时间区间的缓存DB数据
     *
     * @param createDate
     */
    public void deleteDataRefresh(String createDate) {
        LambdaUpdateWrapper<DataRefreshDetailDO> updateWrapper = Wrappers.lambdaUpdate();

        updateWrapper.lt(DataRefreshDetailDO::getVersion, createDate);

        dataRefreshMapper.delete(updateWrapper);
    }
}
